﻿<?php

//power by wshoto.com
if (!defined('IN_IA')) {
    die( 'Access Denied' );
}
require IA_ROOT . '/addons/ewei_shop/defines.php';
require EWEI_SHOP_INC . 'plugin/plugin_processor.php';

class PosterProcessor extends PluginProcessor {
    public function __construct(){
        parent::__construct('poster');
    }

    public function respond($obj = null){
        global $_W;

        $message = $obj->message;
        $msgtype = strtolower($message['msgtype']);
        $event = strtolower($message['event']);
        $obj->member = $this->model->checkMember($message['from']);

        if ($msgtype == 'text' || $event == 'click') {
            return $this->responseText($obj);
        } else {
            if ($msgtype == 'event') {
                if ($event == 'scan') {
                    return $this->responseScan($obj);
                } else {
                    if ($event == 'subscribe') {
                        return $this->responseSubscribe($obj);
                    }
                }
            }
        }
        return true;
    }

    private function responseText($obj){
        global $_W;
        $timeout = 4;
        load()->func('communication');
        ihttp_request($_W['siteroot'] . 'app/index.php?i=' . $_W['uniacid'] . '&c=entry&m=ewei_shop&do=plugin&p=poster&method=build', array( "openid" => $obj->message['from'], "content" => urlencode($obj->message['content']) ), null, $timeout);
        return $this->responseEmpty();
    }

    private function responseEmpty(){
        ob_clean();
        ob_start();
        echo '';
        ob_flush();
        ob_end_flush();
        die( 0 );
    }

    private function responseDefault($obj, $key){
        global $_W;
        return $obj->respText('感谢您的关注!');
    }

    private function responseScan($obj){
        global $_W;
        $openid = $obj->message['from'];
        $sceneid = $obj->message['eventkey'];
        file_put_contents('qrmember.log', "\n#####objscan " . date('Y-m-d H:i:s') . "###########\n", 8);
        file_put_contents('qrmember.log', var_export($obj, 1), 8);
        if (empty( $sceneid )) {
            return $this->responseDefault($obj);
        }
        $qr = $this->model->getQRBySceneid($sceneid);
        if (empty( $qr )) {
            return $this->responseDefault($obj);
        }
        $poster = pdo_fetch('select * from ' . tablename('ewei_shop_poster') . ' where type=4 and isdefault=1 and uniacid=:uniacid limit 1', array( ':uniacid' => $_W['uniacid'] ));
        if (empty( $poster )) {
            return $this->responseDefault($obj);
        }
        $this->model->scanTime($openid, $qr['openid'], $poster);
        $qrmember = m('member')->getMember($qr['openid']);
        $url = trim($poster['respurl']);
        if (empty( $url )) {
            if ($qrmember['isagent'] == 1 && $qrmember['status'] == 1) {
                $url = $_W['siteroot'] . "app/index.php?i={$_W['uniacid']}&c=entry&m=ewei_shop&do=plugin&p=commission&method=myshop&mid=" . $qrmember['id'];
            } else {
                $url = $_W['siteroot'] . "app/index.php?i={$_W['uniacid']}&c=entry&m=ewei_shop&do=shop&mid=" . $qrmember['id'];
            }
        }

        $news = array( array( 'title' => $poster['resptitle'], 'description' => $poster['respdesc'], 'picurl' => tomedia($poster['respthumb']), 'url' => $url ) );

        return $obj->respText('未成功绑定上下级关系。（注意：若您已经为会员，是无法更换上级的。）');
    }

    private function responseSubscribe($obj){
        global $_W;

        file_put_contents('qrmember.log', "\n#####objSubscribe " . date('Y-m-d H:i:s') . "###########\n", 8);
        file_put_contents('qrmember.log', var_export($obj, 1), 8);
        $openid = $obj->message['from'];
        $keys = explode('_', $obj->message['eventkey']);
        $sceneid = isset( $keys[1] ) ? $keys[1] : '';
        if (empty( $sceneid )) {
            return $this->responseDefault($obj, $keys);
        }
        $qr = $this->model->getQRBySceneid($sceneid);

        if (empty( $qr )) {
            return $this->responseDefault($obj);
        }

        $poster = pdo_fetch('select * from ' . tablename('ewei_shop_poster') . ' where type=4 and isdefault=1 and uniacid=:uniacid limit 1', array( ':uniacid' => $_W['uniacid'] ));

        if (empty( $poster )) {
            return $this->responseDefault($obj);
        }

        $member = $obj->member;

        if ($member['isnew']) {
            pdo_update('ewei_shop_poster', array( 'follows' => $poster['follows'] + 1 ), array( 'id' => $poster['id'] ));
        }
        $qrmember = m('member')->getMember($qr['openid']);


        $log = pdo_fetch('select * from ' . tablename('ewei_shop_poster_log') . ' where openid=:openid and posterid=:posterid and uniacid=:uniacid limit 1', array( ':openid' => $openid, ':posterid' => $poster['id'], ':uniacid' => $_W['uniacid'] ));
        file_put_contents('qrmember.log', "\n#####log" . date('Y-m-d H:i:s') . "###########\n", 8);
        file_put_contents('qrmember.log', var_export($log, 1), 8);
        if (empty( $log ) && $openid != $qr['openid']) {//未扫码，而且扫码人和被扫码的人不是同一个人
            $log = array( 'uniacid' => $_W['uniacid'], 'posterid' => $poster['id'], 'openid' => $openid, 'from_openid' => $qr['openid'], 'subcredit' => $poster['subcredit'], 'submoney' => $poster['submoney'], 'reccredit' => $poster['reccredit'], 'recmoney' => $poster['recmoney'], 'createtime' => time() );
            pdo_insert('ewei_shop_poster_log', $log);
            $log['id'] = pdo_insertid();
            $subpaycontent = $poster['subpaycontent'];
            if (empty( $subpaycontent )) {
                $subpaycontent = '您通过 [nickname] 的推广二维码扫码关注的奖励';
            }
            $subpaycontent = str_replace("[nickname]", $qrmember['nickname'], $subpaycontent);
            $recpaycontent = $poster['recpaycontent'];
            if (empty( $recpaycontent )) {
                $recpaycontent = '推荐 [nickname] 扫码关注的奖励';
            }
            $recpaycontent = str_replace("[nickname]", $member['nickname'], $subpaycontent);
            if ($poster['subcredit'] > 0) {
                m('member')->setCredit($openid, 'credit1', $poster['subcredit'], array( 0, '扫码关注积分+' . $poster['subcredit'] ));
            }
            if ($poster['submoney'] > 0) {
                $pay = $poster['submoney'];
                if ($poster['paytype'] == 1) {
                    $pay *= 100;
                }
                m('finance')->pay($openid, $poster['paytype'], $pay, '', $subpaycontent);
            }
            if ($poster['reccredit'] > 0) {
                m('member')->setCredit($qr['openid'], 'credit1', $poster['reccredit'], array( 0, '推荐扫码关注积分+' . $poster['reccredit'] ));
            }
            if ($poster['recmoney'] > 0) {
                $pay = $poster['recmoney'];
                if ($poster['paytype'] == 1) {
                    $pay *= 100;
                }
                m('finance')->pay($qr['openid'], $poster['paytype'], $pay, '', $recpaycontent);
            }
            $sql = 'SELECT COUNT(uid) FROM ' . tablename('mc_members') . ' WHERE `uniacid` = :uniacid';
            $params = array( ':uniacid' => $_W['uniacid'], );

            $count = pdo_fetchcolumn($sql, $params);

            $subtext = '';
            $messageSubtextResult = '';
            if (!empty( $poster['subtext'] )) {
                $subtext = $poster['subtext'];
                if (!empty( $member['nickname'] )) {
                    $nickname = $member['nickname'];
                } else {
                    $nickname = p('poster')->getNickName($openid);
                }
                $subtext = str_replace("[nickname]", $nickname, $subtext);
                $subtext = str_replace("[credit]", $poster['reccredit'], $subtext);
                $subtext = str_replace("[money]", $poster['recmoney'], $subtext);
                $subtext = str_replace("[count]", $count, $subtext);
                if (!empty( $poster['templateid'] )) {
                    $messageSubtextResult = m('message')->sendTplNotice($qr['openid'], $poster['templateid'], array( 'first' => array( 'value' => "", "color" => "#4a5077" ), 'keyword1' => array( 'value' => "系统通知", "color" => "#4a5077" ), 'keyword2' => array( 'value' => "推荐者通知 \r\n", "color" => "#4a5077" ), 'remark' => array( 'value' => "$subtext", "color" => "#4a5077" ) ));
                } else {
                    $messageSubtextResult = m('message')->sendCustomNotice($qr['openid'], $subtext);
                }
            }

            $entrytext = '';
            $messageEntrytextResult = '';
            if (!empty( $poster['entrytext'] )) {
                $entrytext = $poster['entrytext'];
                $entrytext = str_replace("[nickname]", $qrmember['nickname'], $entrytext);
                $entrytext = str_replace("[credit]", $poster['subcredit'], $entrytext);
                $entrytext = str_replace("[money]", $poster['submoney'], $entrytext);
                $entrytext = str_replace("[count]", $count, $entrytext);
                if (!empty( $poster['templateid'] )) {
                    $messageEntrytextResult = m('message')->sendTplNotice($openid, $poster['templateid'], array( 'first' => array( 'value' => "", "color" => "#4a5077" ), 'keyword1' => array( 'value' => "系统通知", "color" => "#4a5077" ), 'keyword2' => array( 'value' => "关注者通知 \r\n", "color" => "#4a5077" ), 'remark' => array( 'value' => "$entrytext 谢谢您对我们的支持！", "color" => "#4a5077" ) ));
                } else {
                    $messageEntrytextResult = m('message')->sendCustomNotice($openid, $entrytext);
                }
            }
        }

        $p = p('commission');
        if ($p) {
            $cset = $p->getSet();
            if (!empty( $cset )) {
                if ($member['isagent'] != 1 && $member['status'] != 1) {
                    $time = time();
                    if (!empty( $poster['bedown'] ) && $qrmember['isagent'] == 1 && $qrmember['status'] == 1 && empty( $member['agentid'] ) && $member['id'] != $qrmember['id']) {
                        $qrmemberid = intval($qrmember['id']);
                        $status = pdo_update('ewei_shop_member', array( 'agentid' => $qrmemberid, 'inviter' => $qrmemberid, 'childtime' => $time ), array( 'id' => $member['id'] ));
                        file_put_contents('qrmember.log', "\n#####qrmember" . date('Y-m-d H:i:s') . "###########\n", 8);
                        $res = pdo_fetch('select * from ' . tablename('ewei_shop_member') . 'where id=:id', array( ':id' => $member['id'] ));
                        file_put_contents('qrmember.log', var_export($res, 1), 8);


                        file_put_contents('qrmember.log', var_export($qrmember, 1), 8);
                        file_put_contents('qrmember.log', "\n#####member " . date('Y-m-d H:i:s') . "###########\n", 8);
                        file_put_contents('qrmember.log', var_export($member, 1), 8);

                        if (!empty( $status )) {
                            $p->sendMessage($qrmember['openid'], array( 'nickname' => $member['nickname'], 'childtime' => $time ), TM_COMMISSION_AGENT_NEW);
                        }
                    }
                    if (!empty( $poster['beagent'] )) {
                        pdo_update('ewei_shop_member', array( 'isagent' => 1, 'status' => intval($cset['become_check']), 'agenttime' => $time ), array( 'id' => $member['id'] ));
                        $p->sendMessage($member['openid'], array( 'nickname' => $member['nickname'], 'agenttime' => $time ), TM_COMMISSION_BECOME);
                    }
                }
            }
        }

        return true;
    }
}